package test.redis;

import redis.clients.jedis.Jedis;
import redis.clients.util.Pool;

/**
 * @Author: sunshuo
 * @Date: 2019/8/9/009 16:51
 * @Version: 1.0
 */
public class RedisUtil {

    Pool<Jedis> jedisPool;

    public byte[] get(byte[] key) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.get(key);
        }
    }

    public byte[] set(byte[] key, byte[] value) {
        try (Jedis jedis = jedisPool.getResource()) {
            jedis.set(key, value);
            return value;
        }
    }

    public byte[] set(byte[] key, byte[] value, int seconds) {
        try (Jedis jedis = jedisPool.getResource()) {
            jedis.set(key, value);
            jedis.expire(key, seconds);
            return value;
        }
    }

    public byte[] set(byte[] key, byte[] value, long unixTime) {
        try (Jedis jedis = jedisPool.getResource()) {
            jedis.set(key, value);
            jedis.expireAt(key, unixTime);
            return value;
        }
    }

    public String get(String key) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.get(key);
        }
    }

    public String set(String key, String value) {
        try (Jedis jedis = jedisPool.getResource()) {
            jedis.set(key, value);
            return value;
        }
    }

    public String set(String key, String value, int seconds) {
        try (Jedis jedis = jedisPool.getResource()) {
            jedis.set(key, value);
            jedis.expire(key, seconds);
            return value;
        }
    }

    public String set(String key, String value, long unixTime) {
        try (Jedis jedis = jedisPool.getResource()) {
            jedis.set(key, value);
            jedis.expireAt(key, unixTime);
            return value;
        }
    }

    public void closePool() {
        if (jedisPool != null) {
            jedisPool.close();
        }
    }
}
